//
//  MqFindBikeBikeDetailViewController.m
//  Meiqi
//
//  Created by lanou on 16/1/20.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqFindBikeBikeDetailViewController.h"
#import "MqFindBikeBikeRankingTableViewCell.h"
#import "MqFindBikeBikeDetailHeaderView.h"
#import "MqFindBikeCommentTableViewCell.h"
#import "MqFindBikeBikeDetailCommentModel.h"
#import "MqSingleton.h"
#import "MqFindBikeBikeDetailRankModel.h"
#import "MqFindBikeBikeDetailHeadModel.h"
#import "MqFBLocalShopTableViewController.h"

#import <UIImageView+WebCache.h>
@interface MqFindBikeBikeDetailViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic ,strong)UITableView *tableView;

@property (nonatomic ,strong)NSMutableArray *commentArray;

@property (nonatomic ,strong)NSMutableArray *rankArray;

@property (nonatomic ,strong)MqFindBikeBikeDetailHeaderView *head;

@end

@implementation MqFindBikeBikeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //self.view.backgroundColor = [UIColor redColor];
    
    [self addTableView];
    [self setUpData];
}

//加载数据
- (void)setUpData
{
    //http://p.biketo.com/api/app/BrandBikes?brand_id=1&cate_id=0&start=0&num=10
    //http://p.biketo.com/api/app/commentList?num=50&product_id=3
    //http://p.biketo.com/api/app/productRank?product_id=3
    //http://p.biketo.com/api/app/product?access_token=b9a4085b1a5cb8cacfcb8f845dceb7ea&lat=31.130020&lon=121.283928&product_id=3
    
    NSString *url = [NSString stringWithFormat:@"http://p.biketo.com/api/app/commentList?num=50&product_id=%@",_product_id];
    [PKRequestManager requestWithUrlString:url parDic:nil Method:GET finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *arr = dic[@"data"][@"data"];
        _commentArray = [NSMutableArray array];
        
        for (NSDictionary *oneDic in arr) {
            MqFindBikeBikeDetailCommentModel *model = [[MqFindBikeBikeDetailCommentModel alloc] init];
            [model setValuesForKeysWithDictionary:oneDic];
            [_commentArray  addObject:model];
        }
        [_tableView reloadData];
    } error:^(NSError *error) {
        
        
    } path:@"BikeDetail"];
    
    
    
    NSString *urlString = [NSString stringWithFormat:@"http://p.biketo.com/api/app/product?access_token=null&lat=%f&lon=%f&product_id=%@",[MqSingleton shareData].lat,[MqSingleton shareData].lon,self.product_id];
    
    [PKRequestManager requestWithUrlString:urlString parDic:nil Method:GET finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSMutableDictionary *headDic = dic[@"data"][@"product"];
        
        MqFindBikeBikeDetailHeadModel *model = [[MqFindBikeBikeDetailHeadModel alloc] init];
        [model setValuesForKeysWithDictionary:headDic];
        
        [_head.imageV sd_setImageWithURL:[NSURL URLWithString:model.photo]];
        
        NSInteger num = [model.star integerValue];
        for (int i = 0; i < num; i ++) {
            UIImageView *                                           starImage = [_head.starLabel viewWithTag:1000 + i];
            starImage.image = [UIImage imageNamed:@"icon_shop_detail_rate@3x"];
        }
        
        _head.gradeLabel.text = [NSString stringWithFormat:@"%@分",model.score];
        
        [_head.addGradeButton setTitle:@"参与评分" forState:(UIControlStateNormal)];
        _head.addGradeButton.backgroundColor= [UIColor redColor];
        
        _head.priceLabel.text = [NSString stringWithFormat:@"%@指导价",model.price];
        
        //  本地经销商button
        //  黑体字
        [_head.agencyButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
        //  设置button的字体颜色
        [_head.agencyButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_head.agencyButton setTitle:[NSString stringWithFormat:@"本地经销商%@家",model.seller_num] forState:(UIControlStateNormal)];
        [_head.agencyButton addTarget:self action:@selector(findShops:) forControlEvents:(UIControlEventTouchUpInside)];
        

        NSArray *array = dic[@"data"][@"rankList"];
        _rankArray = [NSMutableArray array];
        
        for (NSDictionary *twoDic in array) {
            MqFindBikeBikeDetailRankModel *model = [[MqFindBikeBikeDetailRankModel alloc] init];
            [model setValuesForKeysWithDictionary:twoDic];
            [_rankArray addObject:model];
        }
        [_tableView reloadData];
        
    } error:^(NSError *error) {
        
        
    } path:@"BikeDetailRanking"];
    
    
}

//  本地经销商
- (void)findShops:(UIButton *)button{
    
    MqFBLocalShopTableViewController *lsVC = [[MqFBLocalShopTableViewController alloc] init];
    
    lsVC.brand_id = self.brand_id;
    
    NSLog(@"%@",self.brand_id);
    
    [self.navigationController pushViewController:lsVC animated:YES];
}


//添加tableView
- (void)addTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:(UITableViewStyleGrouped)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    
    //添加表头
    _head = [[MqFindBikeBikeDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 300)];
    _tableView.tableHeaderView = _head;
    
    
    [_tableView registerClass:[MqFindBikeBikeRankingTableViewCell class] forCellReuseIdentifier:@"Cell"];
    
    [_tableView registerClass:[MqFindBikeCommentTableViewCell class] forCellReuseIdentifier:@"CommentCell"];
}




#pragma mark -- tableView代理
//分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return  _rankArray.count;
    }
    else
    {
        return _commentArray.count;
    }
}

//设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //根据section判断用那个cell
    if (indexPath.section == 0) {
        MqFindBikeBikeRankingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if (cell == nil) {
            cell = [[MqFindBikeBikeRankingTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"Cell"];
        }
        MqFindBikeBikeDetailRankModel *model = _rankArray[indexPath.row];
        
        cell.model = model;
       return cell;
    }else
    {
        MqFindBikeCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentCell"];
        if (cell == nil) {
            cell = [[MqFindBikeCommentTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"CommentCell"];
            
        }
        MqFindBikeBikeDetailCommentModel *model = _commentArray[indexPath.row];
        cell.model = model;
        
        return cell;
    }
    
}

//分区名
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"品牌排名";
    }else
    {
        return @"最新评论";
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 120;
    }
    else
    {
        return 100;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
